﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Veteran_cms.Models;
using THS.HtmlHelpers;
namespace Veteran_cms.Controllers
{
    [HandleError]
    public class tin_tucController : Controller
    {
        Veterandb ctx = new Veterandb();
        int PageSize = 6;
        public ActionResult trang_chu()
        {
            var model = new HomeListViewModel();
            //get top ten hot news article
            model.HotNewsArticles = ctx.News_Article_SelectTopIsHot(10).ToList();
            //get top ten news article order by created date
            var articles = ctx.news_article.Where(p=>p.status=="Active" && p.show_date!=null).OrderByDescending(p => p.show_date).Take(5).ToList();
            model.ArticleList = articles;
            //get top article by category id 
            var worlds = ctx.News_article_SelectTopByCategory(5, 15).ToList();
            var business = ctx.News_article_SelectTopByCategory(5, 2).ToList();
            var cultures = ctx.News_article_SelectTopByCategory(5, 3).ToList();
            var sports = ctx.News_article_SelectTopByCategory(5, 4).ToList();
            var lifes = ctx.News_article_SelectTopByCategory(5, 5).ToList();
            var sciences = ctx.News_article_SelectTopByCategory(5, 6).ToList();
            var kindness = ctx.News_article_SelectTopByCategory(5, 9).ToList();
            model.TopNewsWorld = worlds;
            model.HotNewsBusiness = business;
            model.TopNewsCulture = cultures;
            model.TopNewsSport = sports;
            model.TopNewsLife = lifes;
            model.TopNewsSciences = sciences;
            model.TopNewsKindness = kindness;
            //blog
            var blogs = ctx.blogs.Where(p => p.is_hot && p.visible).OrderByDescending(p => p.created_date).ToList(); 
            model.TopBlogs = blogs;
            //event
            var events = ctx.events.Where(p=>p.visible).OrderByDescending(p=>p.event_date).ToList();
            model.TopEvents = events;
            return View(model);
        }
        public ActionResult chuyen_muc(int id,int page=1)
        {
            var model = new HomeListViewModel();
            var articles = ctx.news_article.Where(p => p.category_id == id).OrderByDescending(p => p.created_date).ToList();
            model.ArticleList=articles.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.PageInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = articles.Count() };
            //top readed
            model.TopNewsReaded = articles.OrderByDescending(p => p.num_views).Take(5).ToList();
            return View(model);
        }
        public ActionResult paging(int id, int page = 1)
        {
            var model = new HomeListViewModel();
            var articles = ctx.news_article.Where(p => p.category_id == id).OrderByDescending(p => p.created_date).ToList();
            model.ArticleList = articles.Skip((page - 1) * PageSize).Take(PageSize).ToList();
            model.PageInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = articles.Count() };
            return PartialView("UCArticleList",model);
        }
        public ActionResult chi_tiet(int id)
        {
            var model = new DetailsListViewModel();
            news_article article = ctx.news_article.Where(p => p.article_id == id).SingleOrDefault();
            model.CurrentArticle = article;
            return View(model);
        }
    }
}
